package com.whp.yky.yky.service.serviceimpl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whp.yky.yky.entity.poentity.Document;

import com.whp.yky.yky.dao.DocumentMapper;
import com.whp.yky.yky.mapper.ProcessMapper;
import com.whp.yky.yky.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class  DocumentServiceImpl implements DocumentService {


    @Autowired
    private DocumentMapper documentMapper;

    @Autowired
    private ProcessMapper processMapper;


    @Override
    public boolean insertDocumentInfo(Document document){



            try {
                int effecteNum= documentMapper.insertDocumentInfo(document);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("增加成功，主键为："+document.getId());
                    return true;
                }else {
                    throw  new RuntimeException("插入信息失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("插入信息失败了:"+e.getMessage());
            }


    }

    @Override
    public boolean updateDocumentInfo(Document document){

        if (document.getId()!= 0&&!"".equals(document.getId())){
            try {
                int effecteNum= documentMapper.updateDocumentInfo(document);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("修改成功，主键为："+document.getId());
                    return true;
                }else {
                    throw  new RuntimeException("修改失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("修改失败:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }

    }

    @Override
    public IPage<Document> getDocumentInfo(IPage<Document> page, Document document) {
        return page.setRecords(documentMapper.getDocumentInfo(page,document));
    }


    @Override
    public boolean deleteDocumentInfo(int id) {

        try {
            //对mysql数据库做删除操作后，影响的行数effecteNum
            int effecteNum= documentMapper.deleteDocumentInfo(id);
            if (effecteNum>0){
                return true;
            }else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Document getDocumentInfoById(int id) {
        return documentMapper.getDocumentInfoById(id);
    }

    @Override
    public boolean fakeDeleteDocumentInfo(int id) {

            try {
                int effecteNum= documentMapper.fakeDeleteDocumentInfo(id);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    return true;
                }else {
                    throw  new RuntimeException("修改失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("修改失败:"+e.getMessage());
            }
        }

    @Override
    public IPage<Document> getReduceDocumentInfo(IPage<Document> page, Document document) {
        return page.setRecords(documentMapper.getReduceDocumentInfo(page,document)) ;
    }

    @Override
    public List<Document> getDocumentList() {
        return documentMapper.getDocumentList();
    }

    @Override
    public boolean reduceDocumentInfo(int id) {
        try {
            int effecteNum= documentMapper.reduceDeleteDocumentInfo(id);
            if(effecteNum>0){
                //如果影响行数大于0，那么就是增加成功
                return true;
            }else {
                throw  new RuntimeException("修改失败,插入行数有误");
            }
        }catch (Exception e){
            throw  new RuntimeException("修改失败:"+e.getMessage());
        }
    }


}
